package com.test.demo.controller;


import com.test.demo.commons.ResponseResult;
import com.test.demo.domain.UmsAdmin;
import com.test.demo.dto.UmsAdminDto;
import com.test.demo.dto.params.IconParam;
import com.test.demo.dto.params.PasswordParam;
import com.test.demo.dto.params.ProfileParam;
import com.test.demo.service.UmsAdminService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 个人信息管理
 *
 *
 * @author 远 chards_
 * @date: 2020-02-19 14:55
 */
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials = "true")
@RestController
@RequestMapping(value = "profile")
public class ProfileController {


        @Autowired
        private UmsAdminService umsAdminService;

         @Resource
         private BCryptPasswordEncoder passwordEncoder;

    /**
     * 获取个人信息
     *  @SentinelResource(value = "info",fallback = "InfoFallback",fallbackClass = ProfileControllerFallback.class)
     * @param username
     * @return
     */
    @GetMapping(value = "info/{username}")
    public ResponseResult<UmsAdminDto> info(@PathVariable String username){

        UmsAdmin umsAdmin = umsAdminService.get(username);
        UmsAdminDto dto = new UmsAdminDto();
        BeanUtils.copyProperties(umsAdmin,dto);
        return new ResponseResult<UmsAdminDto>(ResponseResult.CodeStatus.OK,"查询用户信息",dto);
    }

    /**
     * 更新个人信息
     * @param profileParam
     * @return
     */
    @PostMapping(value = "update")
    public ResponseResult<Void> update(@RequestBody ProfileParam profileParam){
        UmsAdmin umsAdmin=new UmsAdmin();
        //反射工具类
        BeanUtils.copyProperties(profileParam,umsAdmin);
        int result = umsAdminService.update(umsAdmin);

        //成功
        if (result > 0){
            return new ResponseResult<Void>(ResponseResult.CodeStatus.OK,"更新个人信息成功");
        }
        //失败
        else {
            return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"更新个人信息失败");

        }
    }


    /**
     * 修改密码
     *
     * @param passwordParam {@link PasswordParam}
     * @return {@link ResponseResult}
     */
    @PostMapping(value = "modify/password")
    public ResponseResult<Void> modifyPassword(@RequestBody PasswordParam passwordParam) {
        UmsAdmin umsAdmin = umsAdminService.get(passwordParam.getUsername());

        // 旧密码正确
        if (passwordEncoder.matches(passwordParam.getOldPassword(), umsAdmin.getPassword())) {
            int result = umsAdminService.modifyPassword(umsAdmin.getUsername(), passwordParam.getNewPassword());
            if (result > 0) {
                return new ResponseResult<Void>(ResponseResult.CodeStatus.OK, "修改密码成功");
            }
        }

        // 旧密码错误
        else {
            return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL, "旧密码不匹配，请重试");
        }

        return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL, "修改密码失败");
    }

    /**
     * 修改头像
     * @param iconParam
     * @return
     */
    @PostMapping(value = "modify/icon")
    public  ResponseResult<Void> modifyIcon(@RequestBody IconParam iconParam){
        int result = umsAdminService.modifyIcon(iconParam.getUsername(), iconParam.getPath());
        //成功
        return result>0?  new ResponseResult<Void>(ResponseResult.CodeStatus.OK,"更新头像成功"):new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"更新头像失败");

    }

}
